from django.shortcuts import render
from django.shortcuts import HttpResponse
from chat.models import Contact
from chat.models import Message
import json
# Create your views here.


def add_contact(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode())
        user_id = data.get('user_id')
        my_id = data.get('my_id')
        response = {'userId': user_id, 'myId': my_id}
        return HttpResponse(json.dumps(response))
    else:
        response = {'code': 0, 'msg': 'success'}
        return HttpResponse(json.dumps(response))


def get_contact(request):
    if request.method == 'POST':
        received_data = json.loads(request.body.decode())
        my_id = received_data.get('id')
        sql = "select * from se.contact"
        result = Contact.objects.raw(sql)
        data = []
        for i in result:
            contact = {
                'id': i.id,
                'displayName': i.username,
                'avatar': i.avatar,
                'index': i.index,
                'unread': i.unread,
                'lastSendTime': i.lastSendTime,
                'lastContent': i.lastContent,
            }
            data.append(contact)
        print(data)
        return HttpResponse(data)
    else:
        response = {'code': 0, 'msg': 'success'}
        return HttpResponse(json.dumps(response))


def delete_contact(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode())
        delete_id = data.get('id')
        contact = Contact.objects.get(id=delete_id)
        contact.delete()
        return HttpResponse(delete_id)
    else:
        response = {'code': 0, 'msg': 'success'}
        return HttpResponse(json.dumps(response))


def send_message(request):
    if request.method == 'POST':
        message = Message()
        data = json.loads(request.body.decode())
        message.id = data.get('id')
        message.status = data.get('status')
        message.type = data.get('type')
        message.sendTime = data.get('send_time')
        message.content = data.get('content')
        message.fileSize = data.get('file_size')
        message.fileName = data.get('file_name')
        message.toContactId = data.get('to_contact_id')
        message.fromUser = data.get('from_user')
        message.save()
        return HttpResponse(json.dumps(data))
    else:
        response = {'code': 0, 'msg': 'success'}
        return HttpResponse(json.dumps(response))


def get_message(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode())
        messages_from = Message.objects.filter(fromUser=data.get('id'))
        messages_to = Message.objects.filter(toContactId=data.get('id'))
        messages = []
        for i in messages_from:
            content = {
                'id': i.id,
                'status': i.status,
                'type': i.type,
                'sendTime': i.sendTime,
                'content': i.content,
                'fileSize': i.fileSize,
                'fileName': i.fileName,
                'fromUser': i.fromUser,
                'toContactId': i.toContactId,
            }
            messages.append(content)
        for i in messages_to:
            content = {
                'id': i.id,
                'status': i.status,
                'type': i.type,
                'sendTime': i.sendTime,
                'content': i.content,
                'fileSize': i.fileSize,
                'fileName': i.fileName,
                'fromUser': i.fromUser,
                'toContactId': i.toContactId,
            }
            messages.append(content)
        return HttpResponse(messages)
    else:
        response = {'code': 0, 'msg': 'success'}
        return HttpResponse(json.dumps(response))
